package com.cg.asm.body.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.cg.asm.body.domain.dto.user.UserInfoDTO;
import com.cg.asm.body.domain.vo.user.UserEditVO;
import com.cg.asm.body.domain.vo.user.UserPagedVO;
import com.cg.asm.body.domain.vo.user.UserRoleVO;
import com.cg.asm.body.service.user.IUserInfoService;
import com.cg.asm.body.util.constants.DicEnum;
import com.cg.asm.body.util.constants.EnumGroup;
import com.cg.asm.client.CommonUtil;
import com.cg.asm.client.exception.MybatisExample;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserInfoService userInfoService;

    @ApiOperation(value = "用户列表查询方法（分页）", notes = "用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页尺寸", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sortBy", value = "排序项", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "descending", value = "正序/倒序", dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "filter", value = "过滤条件", dataType = "String", paramType = "query")
    })
    @GetMapping("/pagedlist")
    public JSONObject pagedlist(String pageIndex, String pageSize, String sortBy, boolean descending, String filter) {
        PageInfo<UserPagedVO> contentsPaginator = userInfoService.selectByUserInfo(MybatisExample.initPagedInfo(
                pageIndex,
                pageSize,
                sortBy,
                descending,
                filter
        ));
        return CommonUtil.successJson(contentsPaginator);
    }

    @ApiOperation(value = "获取系统内置帐户类别", notes = "用户")
    @GetMapping("/accountTypeOptions")
    public JSONObject accountTypeOptions() {
        return CommonUtil.successJson(DicEnum.getEnum(EnumGroup.USER_ACCOUNT_TYPE));
    }

    @ApiOperation(value = "获取系统内置用户性别", notes = "用户")
    @GetMapping("/userGenderOptions")
    public JSONObject userGenderOptions() {
        return CommonUtil.successJson(DicEnum.getEnum(EnumGroup.USER_GENDER));
    }

    @ApiOperation(value = "获取系统内置帐户状态", notes = "用户")
    @GetMapping("/accountStatusOptions")
    public JSONObject accountStatusOptions() {
        return CommonUtil.successJson(DicEnum.getEnum(EnumGroup.USER_ACCOUNT_STATUS));
    }

    @ApiOperation(value = "校验用户是否唯一", notes = "用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String", paramType = "path")
    })
    @GetMapping("/userIsUnique/{username}")
    public JSONObject userIsUnique(@PathVariable String username) {
        boolean unique = userInfoService.userIsUnique(username);
        return CommonUtil.successJson(unique);
    }

    @ApiOperation(value = "保存用户信息", notes = "用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userInfoDTO", value = "用户信息对象", required = true, dataType = "UserInfoDTO", paramType = "body")
    })
    @PostMapping("/saveUser")
    public JSONObject saveUser(UserInfoDTO userInfoDTO) {
        try {
            userInfoService.saveUser(userInfoDTO);
            return CommonUtil.successJson();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonUtil.errorJson();
        }
    }


    @ApiOperation(value = "保存编辑的用户信息", notes = "用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userInfoDTO", value = "用户信息对象", required = true, dataType = "UserInfoDTO", paramType = "body")
    })
    @PutMapping("/saveEditUser")
    public JSONObject saveEditUser(UserInfoDTO userInfoDTO) {
        try {
            userInfoService.saveEditUser(userInfoDTO);
            return CommonUtil.successJson();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonUtil.errorJson();
        }
    }

    @ApiOperation(value = "根据用户Id，查询用户详细信息", notes = "用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户Id", required = true, dataType = "String", paramType = "path")
    })
    @GetMapping("/userInfo/{userId}")
    public JSONObject userInfo(@PathVariable String userId) {
        UserEditVO userEditVO = userInfoService.userEditInfo(userId);
        return CommonUtil.successJson(userEditVO);
    }

    @ApiOperation(value = "根据用户Id删除用户信息", notes = "用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "用户Id数组", required = true, dataType = "String[]", paramType = "body")
    })
    @DeleteMapping("/delUser")
    public JSONObject delUser(@RequestBody String[] ids) {
        try {
            userInfoService.delUser(ids);
            return CommonUtil.successJson();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonUtil.errorJson();
        }
    }

    @ApiOperation(value = "根据用户Id更新用户状态", notes = "用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "用户Id数组", required = true, dataType = "String[]", paramType = "body"),
            @ApiImplicitParam(name = "status", value = "更新状态", required = true, dataType = "String", paramType = "path")
    })
    @PutMapping("/updateStatus/{status}")
    public JSONObject delUser(@RequestBody String[] ids, @PathVariable String status) {
        try {
            userInfoService.updateStatus(ids, status);
            return CommonUtil.successJson();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonUtil.errorJson();
        }
    }

    @ApiOperation(value = "根据用户Id重置密码", notes = "用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户Id", required = true, dataType = "String", paramType = "body")
    })
    @PutMapping("/resetPwd")
    public JSONObject resetPwd(String userId) {
        try {
            userInfoService.resetPwd(userId);
            return CommonUtil.successJson();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonUtil.errorJson();
        }
    }

    @ApiOperation(value = "保存选择的角色", notes = "用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userRoleVO", value = "用户角色视图对象", required = true, dataType = "UserRoleVO", paramType = "body")
    })
    @PostMapping("/saveUserRole")
    public JSONObject saveUserRole(@RequestBody UserRoleVO userRoleVO) {
        try {
            userInfoService.saveUserRole(userRoleVO);
            return CommonUtil.successJson();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonUtil.errorJson();
        }
    }

    @ApiOperation(value = "根据用户编码查询用户角色信息", notes = "用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户Id", required = true, dataType = "String", paramType = "path")
    })
    @GetMapping("/roleByUser/{userId}")
    public JSONObject roleByUser(@PathVariable("userId") String userId) {
        List<String> roleIds = userInfoService.roleByUser(userId);
        return CommonUtil.successJson(roleIds);
    }

    @ApiOperation(value = "保存上传的头像", notes = "用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "upAvtar", value = "上传的头像Base64串", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户Id", required = true, dataType = "String", paramType = "query")
    })
    @PostMapping("/saveUpAvatar")
    public JSONObject saveUpAvatar(String upAvatar, String userId) {
        try {
            userInfoService.saveUpAvatar(upAvatar, userId);
            return CommonUtil.successJson();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonUtil.errorJson();
        }
    }
}
